package com.djsearch.adapter;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.djsearch.R;
import com.djsearch.bean.GroupBuyGoods;
import com.djsearch.bean.WeiboBean;
import com.djsearch.util.LinkableUntil;
/**
 * 微博列表listview适配器
 * @author chenyang2
 *
 */
public class WeiboListViewAdapter extends BaseAdapter {
	

	


	private Context mContext;
	private List<WeiboBean> weiboList;
	public WeiboListViewAdapter(Context context,List<WeiboBean> weiboList) {
		mContext = context;
		this.weiboList = weiboList;
	}
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return weiboList.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return weiboList.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	public void refreshData(List<WeiboBean> list) {
		weiboList = list;
		notifyDataSetChanged();
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		
		if (convertView == null) {
			LayoutInflater layoutInflater = ((Activity) mContext).getLayoutInflater();
			convertView = layoutInflater.inflate(R.layout.weibo_listview_item, parent, false);
			convertView.setTag(convertView);
		} else {
			convertView = (View) convertView.getTag();
		}
		//用户头像
		ImageView photoImg = (ImageView)convertView.findViewById(R.id.weiboIcon);
		//用户昵称
		TextView niCheng = (TextView)convertView.findViewById(R.id.niChengText);
		niCheng.setText(weiboList.get(position).getUserId());
		//发表时间
		TextView time = (TextView)convertView.findViewById(R.id.time);
		//04-19 15:13 2013-04-22 18:11:26
		String timeStr = weiboList.get(position).getTime().substring(5, 16);
		time.setText(LinkableUntil.getTime(timeStr));
		
		TextView urlText = (TextView)convertView.findViewById(R.id.url);
		urlText.setText(weiboList.get(position).getUrl());
		
		//距离
		TextView distance = (TextView)convertView.findViewById(R.id.distanceText);
		//微博内容
		TextView content = (TextView)convertView.findViewById(R.id.weiboContent);
		content.setText(weiboList.get(position).getContent());
		
		return convertView;
	}


	/**
	 * 处理时间
	 * @param getTime
	 * @return
	 */
	public String getTime(String getTime){
		 String day_one=getTime.substring(3, 5);
		 String hour_one=getTime.substring(6, 8);
		 String min_one=getTime.substring(9,11);
		 SimpleDateFormat   formatter   =   new   SimpleDateFormat   ("MM-dd HH:mm     ");      
		 Date   curDate   =   new   Date(System.currentTimeMillis());//获取当前时间      
		 String   str   =   formatter.format(curDate);  
		 System.out.println("当前时间是："+str);
		 
		 String day=str.substring(3, 5);
		 String hour=str.substring(6, 8);
		 String min=str.substring(9,11);

		System.out.println("day"+day+"hour"+hour+"min"+min);
		
		if(day_one.equals(day)&&hour_one.equals(hour)){
			int minute_one=Integer.parseInt(min_one);
           int minute=Integer.parseInt(min);
           int x=Math.abs(minute_one-minute);
          if(x==0){
        	  return "刚刚" ;
          }else{
        	  return (x+"分钟前");
          }
           
		}else if(day_one.equals(day)&&!hour_one.equals(hour)){
			int hou_one=Integer.parseInt(hour_one);
           int hou=Integer.parseInt(hour);
           int x=Math.abs(hou_one-hou);
           return (x+"小时前");
		}
		else{
			return getTime;
		}
	}


}
